﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using TDT.Land.Entity;
using TDT.Land.BusinessLogic.Base;
using TDT.Land.Models;
using TDT.Land.Utilities;

namespace TDT.Land.BusinessLogic
{
    /// <summary>
    /// Viết business logic ở đây
    /// - Viết các methods lấy danh sách , insert , update ,delete đối tượng
    /// </summary>
    public class AdminRepo : BaseRepo
    {
        #region Properties
        private static AdminRepo _instanceAdmin = null;
        public static AdminRepo Instance
        {
            get
            {
                if (_instanceAdmin == null)
                {
                    _instanceAdmin = new AdminRepo();
                }
                return _instanceAdmin;
            }
        }
        #endregion

        #region Thành phố / Quận huyện / Phường xã

        /// <summary>Lấy danh sách thành phố</summary>
        /// <returns></returns>
        public List<Cat_ProvinceModel> GetProvinces()
        {

            var provinces = EntityService.Cat_Province.Select(p => new Cat_ProvinceModel
            {
                Id = p.ID,
                Code = p.Code,
                ProvinceName = p.ProvinceName
            }).ToList();
            return provinces;
        }

        public List<Cat_DistrictModel> GetDistricts()
        {
            var districts = EntityService._GetEntityList<Cat_District>()
                .Select(p => new Cat_DistrictModel
                {
                    Code = p.Code,
                    DistrictName = p.DistrictName,
                    Id = p.ID
                }).ToList<Cat_DistrictModel>();
            return districts;
        }

        public List<Cat_DistrictModel> GetDistricts(Guid ProvincesId)
        {            
            var districts = EntityService._GetEntityList<Cat_District>().Where(p => p.ProvinceID == ProvincesId)
                .Select(p => new Cat_DistrictModel
                {
                    Code = p.Code,
                    DistrictName = p.DistrictName,
                    Id = p.ID
                }).ToList();
            return districts;
        }

        public List<Cat_WardModel> GetWards(Guid districtId)
        {
            var wards = EntityService._GetEntityList<Cat_Ward>(p => p.DistrictID == districtId)
                .Select(p => new Cat_WardModel
                {
                    Code = p.Code,
                    WardName = p.WardName,
                    Id = p.ID
                }).ToList<Cat_WardModel>();
            return wards;
        }
        public List<Cat_WardModel> GetWards()
        {
            var wards = EntityService._GetEntityList<Cat_Ward>()
                .Select(p => new Cat_WardModel
                {
                    Code = p.Code,
                    WardName = p.WardName,
                    Id = p.ID
                }).ToList<Cat_WardModel>();
            return wards;
        }
        #endregion

        #region Cat_Land

        /// <summary>Get Cat_Land List</summary>
        /// <returns></returns>
        public List<Cat_LandModel> GetCatLands()
        {
            return GetCatLands(1, 10);
        }

        public List<Cat_LandModel> GetCatLands(int NumPage, int NumRow)
        {
            EntityQuery entityResult = new EntityQuery();
            var catLands = EntityService._GetMultiColume<Cat_Land>(null, out entityResult).OrderByDescending(m => m.DateCreate).Skip((NumPage - 1) * NumRow).Take(NumRow)
                .Select(p => new Cat_LandModel
                {
                    Id = p.ID,
                    Code = p.Code,
                    LandCategoryName = p.LandCategoryName,
                    CategoryType = p.CategoryType,
                    Notes = p.Notes
                }).ToList();
            foreach (var item in catLands)
            {
                item.CategoryType = item.CategoryType.Translate();
            }

            EntityService.ReleaseContext(entityResult);
            return catLands;
        }

        /// <summary>Get CatLand By Id</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Cat_Land GetCatLandById(Guid id)
        {
            var catLand = EntityService._GetEntityList<Cat_Land>(p => p.ID == id).FirstOrDefault();
            return catLand;
        }

        /// <summary>Xoá CatLand (IsDelete == true)</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteCatLand(Cat_Land catLand, string UserName)
        {
            return EntityService._DeleteEntity<Cat_Land>(catLand, UserName);
        }

        public bool CreateCatLand(Cat_Land catLand, string UserName)
        {
            if (catLand != null)
            {
                Guid newId = Guid.NewGuid();
                catLand.ID = newId;
                return EntityService._AddEntity<Cat_Land>(catLand, UserName);
            }
            return false;
        }

        public bool UpdateCatLand(Cat_Land catLand, string UserName)
        {
            return EntityService._UpdateEntity<Cat_Land>(catLand, UserName);
        }


        public List<Cat_LandModel> GetCatLands(string type)
        {
            var catLands = EntityService.Cat_Land.Where(m => m.CategoryType == type)
                .Select(p => new Cat_LandModel
                {
                    Id = p.ID,
                    LandCategoryName = p.LandCategoryName,
                    Notes = p.Notes
                }).ToList();
            return catLands;
        }


        #endregion

        #region User


        public int GetCountItemUser()
        {
            EntityQuery entityResult = new EntityQuery();
            int lstLand = EntityService._GetMultiColume<Sys_User>(null, out entityResult).Count();
            return lstLand;
        }

        public List<Sys_User> GetUsers(int NumPage, int NumRow, int _Permission)
        {
            var lstUser = EntityService._GetEntityList<Sys_User>().Where(d => d.IsLevel < _Permission).OrderByDescending(m => m.DateCreate).Skip((NumPage - 1) * NumRow).Take(NumRow).ToList();
            return lstUser;
        }

        public List<Sys_User> GetUsers(int _Permission)
        {
            return GetUsers(1, 20, _Permission);
        }
        public Sys_User GetUser(Guid ID)
        {
            var User = EntityService._GetEntityList<Sys_User>(m => m.ID == ID).FirstOrDefault();
            return User;
        }
        public bool CreateUser(Sys_User User, string UserName)
        {
            bool result = EntityService._AddEntity<Sys_User>(User, UserName);
            return result;
        }
        public bool UpdateUser(Sys_User user, string UserName)
        {
            bool result = EntityService._UpdateEntity<Sys_User>(user, UserName);
            return result;
        }
        public bool DeleteUser(Guid userID , string UserName)
        {
            var modelUser = GetUser(userID);
            bool result = EntityService._DeleteEntity<Sys_User>(modelUser,UserName);
            return result;
        }
        public bool CheckUserName(string userName)
        {
            bool statusChange = false;
            var User = (new EntityQuery())._GetEntityList<Sys_User>(m => m.LoginName == userName).FirstOrDefault();
            if (User != null)
                statusChange = true;

            return statusChange;

        }
        #endregion

        #region Logon
        public Sys_User Login(string UserNameInput, string PassWordInput)
        {
            var User = (new EntityQuery())._GetEntityList<Sys_User>(m => m.LoginName == UserNameInput && m.Password == PassWordInput).FirstOrDefault();
            //if (User != null)
            //{
            //    Extent.UserNameFull = User.UserName;
            //    Extent.UserName = User.LoginName;
            //    Extent.UserID = User.ID;
            //    Extent.UserType = User.UserType;
            //    Extent.UserPermission = User.IsLevel ?? 0;
            //    return true;
            //}
            //else
            //{
            //    Extent.UserNameFull = User.UserName;
            //    Extent.UserName = string.Empty;
            //    Extent.UserID = Guid.Empty;
            //    Extent.UserType = string.Empty;
            //    Extent.UserPermission = 0;
            //    return false;
            //}
            return User;

        }
        public void Logout()
        {           

            //Extent.UserNameFull = string.Empty;
            //Extent.UserName = string.Empty;
            //Extent.UserID = Guid.Empty;
            //Extent.UserType = string.Empty;
            //Extent.UserPermission = 0;

        }
        #endregion

        #region Building - Bài viết

        public List<Pro_Building> GetProBuildings()
        {
            //  var building = EntityService._GetEntityList<Pro_Building>().ToList();
            var building = EntityService.Pro_Building.ToList();
            //.Select(p => new Pro_BuildingModel
            //{
            //    BuildingName = p.BuildingName,
            //    Description = p.Description,
            //    ShortDescription = p.ShortDescription,
            //    DateUpdate = p.DateUpdate,
            //    Id = p.ID,
            //    IsDelete = p.IsDelete
            //}).ToList();
            return building;
        }

        public Pro_Building GetProBuildingById(Guid id)
        {
            
            var proBuilding = EntityService.Pro_Building.Where(p => p.ID == id).FirstOrDefault();
            EntityService.Refresh(System.Data.Objects.RefreshMode.ClientWins, proBuilding);
            return proBuilding;
        }


        public Pro_Building GetProBuildingByCode(string bcode, string UserName)
        {
            var proBuilding = EntityService._GetEntityList<Pro_Building>(p => p.Code == bcode).FirstOrDefault();
            if (proBuilding != null)
            {
                var count_view = proBuilding.CountView;
                if (count_view !=null)
                    proBuilding.CountView = count_view + 1;
                else
                    proBuilding.CountView = 1;
                UpdateProBuilding(proBuilding, UserName);
            }
            return proBuilding;
        }

        public bool RemoveProBuilding(Guid id, string UserName)
        {
            var proBuildings = EntityService._GetEntityList<Pro_Building>(p => p.ID == id).FirstOrDefault();
            var isDeleteSuccess = EntityService._DeleteEntity<Pro_Building>(proBuildings, UserName);
            return isDeleteSuccess;
        }

        public Pro_Building AddProBuilding(Pro_Building proBuildingModel,string UserName)
        {
            //  var cat_LandModel = new Pro_BuildingModel();

            if (proBuildingModel != null)
            {
                //Guid newId = Guid.NewGuid();               
                //var proBuilding = new Pro_Building
                //{
                //    ID = Guid.NewGuid(),
                //    BuildingName = proBuildingModel.BuildingName,
                //    Description = proBuildingModel.Description,
                //    ShortDescription = proBuildingModel.ShortDescription,
                //    DateCreate = DateTime.Now,
                //    DateUpdate = DateTime.Now
                //};                
                //proBuildingModel.Id = newId;
                //cat_LandModel = proBuildingModel;

                // bool isSuccess =   EntityService._AddEntity<Pro_Building>(proBuildings);
                var isAddingSuccess = EntityService._AddEntity<Pro_Building>(proBuildingModel,UserName);
                //if (isAddingSuccess)
                //{
                //    return cat_LandModel;
                //}

            }
            return proBuildingModel;
        }

        public bool UpdateProBuilding(Pro_Building proBuilding, string UserName)
        {
            bool isSuccess = EntityService._UpdateEntity<Pro_Building>(proBuilding, UserName);
            return isSuccess;
        }


        public List<Pro_Building> GetProBuildingByType(string BType)
        {
            var proBuilding = EntityService._GetEntityList<Pro_Building>(p => p.BuildingType == BType)
                .Select(p => new Pro_Building
                {
                    BuildingName = p.BuildingName,
                    Description = p.Description,
                    ShortDescription = p.ShortDescription,
                    DateUpdate = p.DateUpdate,
                    BuildingType = p.BuildingType,
                    ImageFile = p.ImageFile,
                    UserCreate = p.UserCreate,
                    UserUpdate = p.UserUpdate,
                    ID = p.ID,
                    IsDelete = p.IsDelete,
                    IsSlideShow = p.IsSlideShow,
                    Code = p.Code,
                    DateCreate = p.DateCreate,
                    CountView = p.CountView
                }).ToList();
            return proBuilding;
        }


        #endregion

        #region Land - Căn Hộ
        public List<Pro_LandModel> GetProLands()
        {
            return GetProLands(1, 20);
        }

        public List<Pro_LandModel> GetProLandShowTop()
        {
            EntityQuery entityResult = new EntityQuery();
            var lstLand = EntityService._GetMultiColume<Pro_Land>(m => m.IsShowTop != null && m.IsShowTop == true, out entityResult).OrderByDescending(m => m.DateCreate)
                .Select(m => new Pro_LandModel
                {
                    LandName = m.LandName,
                    Notes = m.Notes,
                    LandStatus = m.LandStatus,
                    LandType = m.Cat_Land.LandCategoryName,
                    LandBDSType = m.Cat_Land1.LandCategoryName,
                    Code = m.Code,
                    LandPrice = m.LandPrice,
                    LandImage = m.LandImage,
                    Id = m.ID,
                    CreateDate = m.DateCreate,
                    UpdateDate =  m.DateUpdate,
                    url = m.url
                }).ToList();
            EntityService.ReleaseContext(entityResult);

            foreach (var item in lstLand)
            {
                item.LandStatus = item.LandStatus.Translate();
            }
            return lstLand;
        }
        public Pro_Land GetProLandById(Guid id)
        {
            var proLand = EntityService._GetEntityList<Pro_Land>(p => p.ID == id).FirstOrDefault();
            return proLand;
        }
        public Pro_LandModel GetProLandViewById(Guid id)
        {
            EntityQuery entityResult = new EntityQuery();
            var land = EntityService._GetMultiColume<Pro_Land>(m => m.ID == id, out entityResult).OrderByDescending(m => m.DateCreate)
                .Select(m => new Pro_LandModel
                {
                    LandName = m.LandName,
                    url = m.url,
                    Notes = m.Notes,
                    ProvinceName = m.Cat_Province.ProvinceName,
                    DistrictName = m.Cat_District.DistrictName,
                    WardName = m.Cat_Ward.WardName,
                    Address = m.Address,
                    LandStatus = m.LandStatus,
                    LandDemand = m.LandDemand,
                    Description = m.Description,
                    LandSquare = m.LandSquare,
                    LandStatusDate = m.LandStatusDate,
                    LandOrient = m.LandOrient,
                    Furniture = m.Furniture,
                    Floor = m.Floor,
                    Rooms = m.Rooms,
                    LandType = m.Cat_Land.LandCategoryName,
                    LandBDSType = m.Cat_Land1.LandCategoryName,
                    Code = m.Code,
                    LandPrice = m.LandPrice,
                    Id = m.ID,
                    CreateDate = m.DateCreate,
                    UpdateDate = m.DateUpdate,
                    LandImage = m.LandImage
                }).FirstOrDefault();
            EntityService.ReleaseContext(entityResult);
            if (land != null)
            {
                land.LandStatus = land.LandStatus.Translate();
            }
            return land;

        }

        //NguyenDuy add methos
        public Pro_LandModel GetProLandViewByUrl(string url)
        {
            EntityQuery entityResult = new EntityQuery();
            var land = EntityService._GetMultiColume<Pro_Land>(m => m.url == url, out entityResult).OrderByDescending(m => m.DateCreate)
                .Select(m => new Pro_LandModel
                {
                    LandName = m.LandName,
                    url = m.url,
                    Notes = m.Notes,
                    ProvinceName = m.Cat_Province.ProvinceName,
                    DistrictName = m.Cat_District.DistrictName,
                    WardName = m.Cat_Ward.WardName,
                    Address = m.Address,
                    LandStatus = m.LandStatus,
                    LandDemand = m.LandDemand,
                    Description = m.Description,
                    LandSquare = m.LandSquare,
                    LandStatusDate = m.LandStatusDate,
                    LandOrient = m.LandOrient,
                    Furniture = m.Furniture,
                    Floor = m.Floor,
                    Rooms = m.Rooms,
                    LandType = m.Cat_Land.LandCategoryName,
                    LandBDSType = m.Cat_Land1.LandCategoryName,
                    Code = m.Code,
                    LandPrice = m.LandPrice,
                    Id = m.ID,
                    CreateDate = m.DateCreate,
                    UpdateDate = m.DateUpdate,
                    LandImage = m.LandImage
                }).FirstOrDefault();
            EntityService.ReleaseContext(entityResult);
            if (land != null)
            {
                land.LandStatus = land.LandStatus.Translate();
            }
            return land;

        }

        public int GetCountItemLand()
        {
            EntityQuery entityResult = new EntityQuery();
            int lstLand = EntityService._GetMultiColume<Pro_Land>(null, out entityResult).Count();
            return lstLand;
        }

        public List<Pro_LandModel> GetProLands(int NumPage, int NumRow)
        {
            EntityQuery entityResult = new EntityQuery();
            var lstLand = EntityService._GetMultiColume<Pro_Land>(null, out entityResult).OrderByDescending(m => m.DateCreate).Skip((NumPage - 1) * NumRow).Take(NumRow)
                .Select(m => new Pro_LandModel
                {
                    LandName = m.LandName,  
                    url = m.url,
                    Notes = m.Notes,
                    ProvinceName = m.Cat_Province.ProvinceName,
                    DistrictName = m.Cat_District.DistrictName,
                    WardName = m.Cat_Ward.WardName,
                    Address = m.Address,
                    LandStatus = m.LandStatus,
                    LandDemand = m.LandDemand,
                    Description = m.Description,
                    LandSquare = m.LandSquare,
                    LandStatusDate = m.LandStatusDate,
                    LandOrient = m.LandOrient,
                    Furniture = m.Furniture,
                    Floor = m.Floor,
                    Rooms = m.Rooms,
                    LandType = m.Cat_Land.LandCategoryName,
                    LandBDSType = m.Cat_Land1.LandCategoryName,
                    Code = m.Code,
                    LandPrice = m.LandPrice,
                    LandImage = m.LandImage,
                    Id = m.ID,
                    CreateDate =  m.DateCreate,
                    UpdateDate =  m.DateUpdate,
                    UserCreate = m.UserCreate
                }).ToList();
            EntityService.ReleaseContext(entityResult);

            foreach (var item in lstLand)
            {
                item.LandStatus = item.LandStatus.Translate();
            }
            return lstLand;
        }

        public List<Pro_LandModel> GetProLandsBySearch(int NumPage, int NumRow, Guid Province, Guid District, Guid Ward, Guid HomeType, Guid BDSType)
        {
            EntityQuery entityResult = new EntityQuery();
            var QuerySearch = EntityService._GetMultiColume<Pro_Land>(null, out entityResult);
            if (Province != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.ProvinceId == Province);
            }
            if (District != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.DistrictId == District);
            }
            if (Ward != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.WardId == Ward);
            }
            if (HomeType != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.LandTypeId == HomeType);
            }
            if (BDSType != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.LandTypeBDSId == BDSType);
            }
            var lstLand = QuerySearch.OrderByDescending(m => m.DateCreate).Skip((NumPage - 1) * NumRow).Take(NumRow)
                .Select(m => new Pro_LandModel
                {
                    LandName = m.LandName,
                    Notes = m.Notes,
                    ProvinceName = m.Cat_Province.ProvinceName,
                    DistrictName = m.Cat_District.DistrictName,
                    WardName = m.Cat_Ward.WardName,
                    Address = m.Address,
                    LandStatus = m.LandStatus,
                    LandDemand = m.LandDemand,
                    Description = m.Description,
                    LandSquare = m.LandSquare,
                    LandStatusDate = m.LandStatusDate,
                    LandOrient = m.LandOrient,
                    Furniture = m.Furniture,
                    Floor = m.Floor,
                    Rooms = m.Rooms,
                    LandType = m.Cat_Land.LandCategoryName,
                    LandBDSType = m.Cat_Land1.LandCategoryName,
                    Code = m.Code,
                    LandPrice = m.LandPrice,
                    Id = m.ID,
                    url = m.url
                }).ToList();
            EntityService.ReleaseContext(entityResult);

            foreach (var item in lstLand)
            {
                item.LandStatus = item.LandStatus.Translate();
            }

            return lstLand;
        }


        public bool DeleteProLand(Guid LandId, string UserName)
        {
            var proLands = EntityService._GetEntityList<Pro_Land>(p => p.ID == LandId).FirstOrDefault();
            bool isSuccess = EntityService._DeleteEntity<Pro_Land>(proLands, UserName);
            return isSuccess;
        }
        public bool CreateProLand(Pro_Land proLands, string UserName)
        {
            bool isSuccess = EntityService._AddEntity<Pro_Land>(proLands, UserName);
            return isSuccess;
        }
        public bool UpdateProLand(Pro_Land proLands, string UserName)
        {
            bool isSuccess = EntityService._UpdateEntity<Pro_Land>(proLands, UserName);
            return isSuccess;
        }
        #endregion

        #region CusLand - Căn Hộ Customer
        public List<Cus_LandModel> GetCusLands()
        {
            return GetCusLands(1, 10);
        }
        public Cus_Land GetCusLandById(Guid id)
        {
            var CusLand = EntityService._GetEntityList<Cus_Land>(p => p.ID == id).FirstOrDefault();
            return CusLand;
        }
        public List<Cus_Land> GetCusLandById(List<Guid> ids)
        {
            var CusLand = EntityService._GetEntityList<Cus_Land>(p =>ids.Contains( p.ID )).ToList();
            return CusLand;
        }
        public Cus_LandModel GetCusLandViewById(Guid id)
        {
            EntityQuery entityResult = new EntityQuery();
            var land = EntityService._GetMultiColume<Cus_Land>(m => m.ID == id, out entityResult).OrderByDescending(m => m.DateCreate)
                .Select(m => new Cus_LandModel
                {
                    Id= m.ID,
                    PuposeType = m.PuposeType,
                    Area = m.Area,
                    Currency = m.Currency,
                    IsShowMoney = m.IsShowMoney,
                    TotalCost = m.TotalCost,
                    CostPerMeter = m.CostPerMeter,
                    Direction = m.Direction,
                    StreetWidth = m.StreetWidth,
                    LegalPaper = m.LegalPaper,
                    NumFloor = m.NumFloor,
                    NumBedRoom = m.NumBedRoom,
                    NumRestRoom = m.NumRestRoom,
                    NumAddress = m.NumAddress,
                    StreetAddress = m.StreetAddress,
                    NameContact = m.NameContact,
                    CellPhoneContact = m.CellPhoneContact,
                    HomePhoneContact = m.HomePhoneContact,
                    EmailContact = m.EmailContact,
                    BDSLandName = m.Cat_Land.LandCategoryName,
                    BDSTypeName = m.Cat_BDSType.BDSTypeName,
                    DictrictName = m.Cat_District.DistrictName,
                    ProvinceName = m.Cat_Province.ProvinceName,
                    WardName = m.Cat_Ward.WardName,
                    UserCreate = m.UserCreate,
                    UserUpdate = m.UserUpdate,
                    DateCreate = m.DateCreate,
                    DateUpdate = m.DateUpdate,
                    Title = m.Title,
                    url = m.url,
                    CusLand_Image = m.CusLand_Image,
                    CusLand_Body = m.CusLand_Body
                }).FirstOrDefault();
            EntityService.ReleaseContext(entityResult);
          
            return land;

        }
        public List<Cus_LandModel> GetCusLands(int NumPage, int NumRow)
        {
            EntityQuery entityResult = new EntityQuery();
            var lstLand = EntityService._GetMultiColume<Cus_Land>(null, out entityResult).OrderByDescending(m => m.DateCreate).Skip((NumPage - 1) * NumRow).Take(NumRow)
                .Select(m => new Cus_LandModel
                {
                    Id = m.ID,
                    PuposeType = m.PuposeType,
                    Area = m.Area,
                    Currency = m.Currency,
                    IsShowMoney = m.IsShowMoney,
                    TotalCost = m.TotalCost,
                    CostPerMeter = m.CostPerMeter,
                    Direction = m.Direction,
                    StreetWidth = m.StreetWidth,
                    LegalPaper = m.LegalPaper,
                    NumFloor = m.NumFloor,
                    NumBedRoom = m.NumBedRoom,
                    NumRestRoom = m.NumRestRoom,
                    NumAddress = m.NumAddress,
                    StreetAddress = m.StreetAddress,
                    NameContact = m.NameContact,
                    CellPhoneContact = m.CellPhoneContact,
                    HomePhoneContact = m.HomePhoneContact,
                    EmailContact = m.EmailContact,
                    BDSLandName = m.Cat_Land.LandCategoryName,
                    BDSTypeName = m.Cat_BDSType.BDSTypeName,
                    DictrictName = m.Cat_District.DistrictName,
                    ProvinceName = m.Cat_Province.ProvinceName,
                    WardName = m.Cat_Ward.WardName,
                    UserCreate = m.UserCreate,
                    UserUpdate = m.UserUpdate,
                    DateCreate = m.DateCreate,
                    DateUpdate = m.DateUpdate,
                    Title = m.Title,
                    url = m.url,
                    CusLand_Image = m.CusLand_Image,
                    CusLand_Body = m.CusLand_Body

                }).ToList();
            EntityService.ReleaseContext(entityResult);
            return lstLand;
        }

        public List<Cus_LandModel> GetCusLandsBySearch(int NumPage, int NumRow, Guid Province, Guid District, Guid Ward, Guid HomeType, Guid BDSType)
        {
            EntityQuery entityResult = new EntityQuery();
            var QuerySearch = EntityService._GetMultiColume<Cus_Land>(null, out entityResult);
            if (Province != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.ProvinceID == Province);
            }
            if (District != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.DictrictID == District);
            }
            if (Ward != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.WardID == Ward);
            }
            if (HomeType != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.LandID == HomeType);
            }
            if (BDSType != Guid.Empty)
            {
                QuerySearch = QuerySearch.Where(m => m.BDSTypeID == BDSType);
            }
            var lstLand = QuerySearch.OrderByDescending(m => m.DateCreate).Skip((NumPage - 1) * NumRow).Take(NumRow)
                .Select(m => new Cus_LandModel
                {
                    Id = m.ID,
                    PuposeType = m.PuposeType,
                    Area = m.Area,
                    Currency = m.Currency,
                    IsShowMoney = m.IsShowMoney,
                    TotalCost = m.TotalCost,
                    CostPerMeter = m.CostPerMeter,
                    Direction = m.Direction,
                    StreetWidth = m.StreetWidth,
                    LegalPaper = m.LegalPaper,
                    NumFloor = m.NumFloor,
                    NumBedRoom = m.NumBedRoom,
                    NumRestRoom = m.NumRestRoom,
                    NumAddress = m.NumAddress,
                    StreetAddress = m.StreetAddress,
                    NameContact = m.NameContact,
                    CellPhoneContact = m.CellPhoneContact,
                    HomePhoneContact = m.HomePhoneContact,
                    EmailContact = m.EmailContact,
                    BDSLandName = m.Cat_Land.LandCategoryName,
                    BDSTypeName = m.Cat_BDSType.BDSTypeName,
                    DictrictName = m.Cat_District.DistrictName,
                    ProvinceName = m.Cat_Province.ProvinceName,
                    WardName = m.Cat_Ward.WardName,
                    UserCreate = m.UserCreate,
                    UserUpdate = m.UserUpdate,
                    DateCreate = m.DateCreate,
                    DateUpdate = m.DateUpdate,
                    Title = m.Title,
                    url = m.url,
                    CusLand_Image = m.CusLand_Image,
                    CusLand_Body = m.CusLand_Body
                }).ToList();
            EntityService.ReleaseContext(entityResult);
            return lstLand;
        }


        public bool DeleteCusLand(Cus_Land CusLands, string UserName)
        {
            bool isSuccess = EntityService._DeleteEntity<Cus_Land>(CusLands, UserName);
            return isSuccess;
        }
        public bool DeleteCusLand(List<Guid> lstCusLandIds, string UserName)
        {
            var CusLand = EntityService._GetEntityList<Cus_Land>(p => lstCusLandIds.Contains(p.ID)).ToList();
            bool isSuccess = EntityService._DeleteEntity<Cus_Land>(CusLand, UserName);
            return isSuccess;
        }
        public bool CreateCusLand(Cus_Land CusLands, string UserName)
        {
            bool isSuccess = EntityService._AddEntity<Cus_Land>(CusLands, UserName);
            return isSuccess;
        }
        public bool UpdateCusLand(Cus_Land CusLands, string UserName)
        {
            bool isSuccess = EntityService._UpdateEntity<Cus_Land>(CusLands, UserName);
            return isSuccess;
        }
        #endregion

        #region BDSType - Loai BDS
        public List<Cat_BDSTypeModel> GetBDSTypes()
        {
            return GetBDSTypes(1, 10);
        }
        public List<Cat_BDSType> GetBDSTypeShowTop()
        {
            EntityQuery entityResult = new EntityQuery();
            var lstLand = EntityService._GetMultiColume<Cat_BDSType>(null, out entityResult).OrderByDescending(m => m.DateCreate).ToList();
            EntityService.ReleaseContext(entityResult);
            return lstLand;
        }
     
        public Cat_BDSType GetBDSTypeById(Guid id)
        {
            var BDSType = EntityService._GetEntityList<Cat_BDSType>(p => p.ID == id).FirstOrDefault();
            return BDSType;
        }
        public Cat_BDSType GetBDSTypeViewById(Guid id)
        {
            EntityQuery entityResult = new EntityQuery();
            var land = EntityService._GetMultiColume<Cat_BDSType>(m => m.ID == id, out entityResult).OrderByDescending(m => m.DateCreate).FirstOrDefault();
            EntityService.ReleaseContext(entityResult);
            return land;
        }
        public List<Cat_BDSTypeModel> GetBDSTypes(int NumPage, int NumRow)
        {
            EntityQuery entityResult = new EntityQuery();
            var lstLand = EntityService._GetMultiColume<Cat_BDSType>(null, out entityResult).OrderByDescending(m => m.DateCreate).Skip((NumPage - 1) * NumRow).Take(NumRow)
                .Select(m => new Cat_BDSTypeModel() { Id = m.ID, BDSTypeName = m.BDSTypeName, Code = m.Code, BDSLandName = m.Cat_Land.LandCategoryName, Note = m.Note, DateCreate = m.DateCreate, DateUpdate = m.DateUpdate, UserCreate = m.UserCreate, UserUpdate = m.UserUpdate })
               .ToList();
            EntityService.ReleaseContext(entityResult);
            return lstLand;
        }
        public bool DeleteBDSType(Cat_BDSType BDSTypes,string UserName)
        {
            bool isSuccess = EntityService._DeleteEntity<Cat_BDSType>(BDSTypes, UserName);
            return isSuccess;
        }
        public bool CreateBDSType(Cat_BDSType BDSTypes, string UserName)
        {
            bool isSuccess = EntityService._AddEntity<Cat_BDSType>(BDSTypes, UserName);
            return isSuccess;
        }
        public bool UpdateBDSType(Cat_BDSType BDSTypes, string UserName)
        {
            bool isSuccess = EntityService._UpdateEntity<Cat_BDSType>(BDSTypes, UserName);
            return isSuccess;
        }
        #endregion
        
        #region Menu

        public List<Menu> GetAllParentMenu(string siteView)
        {
            var lstMenu = EntityService.Menu.Where(d => d.ParentID == 0 && d.SiteView == siteView).OrderBy(d => d.MenuOrder).ToList();
            return lstMenu;
        }

        public List<Menu> GetAllMenu(string siteView)
        {
            var lstMenu = EntityService.Menu.Where(d => d.SiteView == siteView).ToList();
            EntityService.Refresh(System.Data.Objects.RefreshMode.ClientWins, lstMenu);
            return lstMenu;
        }

        public List<Menu> GetMenuChil(int parentID)
        {
            var lstMenu = EntityService.Menu.Where(d => d.ParentID == parentID).OrderBy(d => d.MenuOrder).ToList();
            EntityService.Refresh(System.Data.Objects.RefreshMode.ClientWins, lstMenu);
            return lstMenu;
        }

        public Menu GetMenuById(int menuId)
        {
            var lstMenu = EntityService.Menu.Where(d => d.MenuID == menuId).OrderBy(d => d.MenuOrder).FirstOrDefault();
           // EntityService.Refresh(System.Data.Objects.RefreshMode.ClientWins, lstMenu);
            return lstMenu;
        }

        public bool addNewMenu(Menu _itemMenu, string UserName)
        {
            bool status = false;
            if (_itemMenu != null)
            {
                var isAddingSuccess = EntityService._AddEntity<Menu>(_itemMenu, UserName);
                status = isAddingSuccess;
            }
            return status;
        }

        public bool UpdateMenu(Menu _itemMenu, string UserName)
        {
            bool isSuccess = EntityService._UpdateEntity<Menu>(_itemMenu, UserName);
            return isSuccess;
        }



        #endregion
        
        #region Event

        public Sys_Event GetEventById(int id)
        {

            var modelEvent = EntityService._GetEntityList<Sys_Event>(m=>m.id==id).FirstOrDefault();
           // EntityService.Refresh(System.Data.Objects.RefreshMode.ClientWins, modelEvent);
            return modelEvent;
        }

        public List<Sys_Event> GetAllEvent()
        {
            var lstEvent = EntityService._GetEntityList<Sys_Event>();
            return lstEvent;
        }

        public bool InsertEvent(Sys_Event eventItem, string UserName)
        {
            bool status = false;
            if (eventItem != null)
            {
                int indexOrder = 0;
                eventItem.createDate = DateTime.Now;
                var currentEvent = EntityService._GetEntityList<Sys_Event>().OrderByDescending(p => p.order).FirstOrDefault();
               // EntityService.Refresh(System.Data.Objects.RefreshMode.ClientWins, currentEvent);
                if (currentEvent != null)
                {
                    if (currentEvent.order != null)
                        indexOrder = (int)currentEvent.order + 1;
                }
                eventItem.order = indexOrder;
                var isAddingSuccess = EntityService._AddEntity<Sys_Event>(eventItem,  UserName);
                status = isAddingSuccess;
            }
            return status;
        }


        public bool DeleteEvent(int id, string UserName)
        {
            var modelEvent = EntityService._GetEntityList<Sys_Event>(p => p.id == id).FirstOrDefault();
                //EntityService.Sys_Event.Where(p => p.id == id).FirstOrDefault();
            var isDeleteSuccess = EntityService._DeleteEntity<Sys_Event>(modelEvent, UserName);
            return isDeleteSuccess;
        }

        public bool UpdateEvent(Sys_Event eventItem, string UserName)
        {
            bool isSuccess = EntityService._UpdateEntity<Sys_Event>(eventItem, UserName);
            return isSuccess;
        }

        public Sys_Event ViewEvent(string alt_event)
        {
            var modelEvent = EntityService.Sys_Event.Where(p => p.IsShow == true && p.alt == alt_event).FirstOrDefault();
            return modelEvent;
        }


        public Sys_Event showTopEvent()
        {
            var modelEvent = EntityService._GetEntityList<Sys_Event>().Where(p => p.IsShow == true).OrderByDescending(p=>p.order).ThenByDescending(p=>p.createDate).FirstOrDefault();
            //EntityService.Refresh(System.Data.Objects.RefreshMode.ClientWins, modelEvent);
            return modelEvent;
        }


        public bool checkShowEvent()
        {
            bool showEvent = false;
            var modelEvent = EntityService.Sys_Event.Where(p => p.IsShow == true).OrderByDescending(p => p.order).ThenByDescending(p => p.createDate).FirstOrDefault();
           // EntityService.Refresh(System.Data.Objects.RefreshMode.ClientWins, modelEvent);
            if (modelEvent != null)
            {
                if (modelEvent.EventBody != null)                
                    showEvent = true;                
            }

            return showEvent;
        }

        #endregion


    }
}
